<template>
  <!-- 引入公共样式 -->
  <div class="app-container">
    <el-form :inline="true" ref="queryForm" :model="queryParams">
      <!-- 搜索区域开始 -->
      <el-form-item label="角色名称" prop="roleName">
        <el-input v-model="queryParams.roleName" placeholder="请输入角色名称" size="small" style="width: 240px"/>
      </el-form-item>

      <el-form-item label="权限字符" prop="roleKey">
        <el-input v-model="queryParams.roleKey" placeholder="请输入权限字符" size="small" style="width: 240px"/>
      </el-form-item>

      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态" size="small" style="width: 240px"/>
      </el-form-item>

      <el-form-item label="创建时间">
        <el-date-picker v-model="dateRange"
                        size="small"
                        style="width: 240px"
                        type="daterange"
                        range-separator="-"
                        value-format="yyyy-MM-dd"
                        start-placeholder="开始时间"
                        end-placeholder="结束时间"/>
      </el-form-item>

      <el-form-item>
        <el-button icon="el-icon-search" type="primary" size="small" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="small" @click="resetQuery">重置</el-button>
      </el-form-item>
      <!-- 搜索区域结束 -->

      <!-- 按钮区域开始 -->
      <el-row :gutter="10">
        <el-col :span="1.5">
          <el-button icon="el-icon-plus" type="success" size="mini" plain @click="handleAdd">新增</el-button>
        </el-col>

        <el-col :span="1.5">
          <el-button icon="el-icon-edit" type="info" size="mini" plain>修改</el-button>
        </el-col>

        <el-col :span="1.5">
          <el-button icon="el-icon-delete" type="danger" size="mini" plain>删除</el-button>
        </el-col>

        <el-col :span="1.5">
          <el-button icon="el-icon-download" type="warning" size="mini" plain>导出</el-button>
        </el-col>

        <!-- 右侧按钮组 -->
        <right-tool-bar></right-tool-bar>
      </el-row>
      <!-- 按钮区域结束 -->

      <!-- 表格区域开始 -->
      <el-table :data="roleList">
        <el-table-column type="selection" width="55" align="center"></el-table-column>
        <el-table-column label="角色编号" prop="roleId" width="120"></el-table-column>
        <el-table-column label="角色名称" prop="roleName" width="150"></el-table-column>
        <el-table-column label="权限字符" prop="roleKey" width="150"></el-table-column>
        <el-table-column label="显示顺序" prop="roleSort" width="100"></el-table-column>
        <el-table-column label="状态" width="100" prop="status" min-width="100">
          <template slot-scope="scope">
            <!--
            <el-tag :type="scope.row.status === '0' ? 'success' : 'danger'">
              {{ scope.row.status === '0' ? '正常' : '停用' }}
            </el-tag>
            -->
            <el-switch v-model="scope.row.status" active-value="0" inactive-value="1"></el-switch>
          </template>
        </el-table-column>
        <el-table-column label="创建时间" width="180" prop="createTime"></el-table-column>
        <el-table-column label="操作" align="center">
          <template slot-scope="scope">
            <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)">修改</el-button>
            <el-button size="mini" type="text" icon="el-icon-delete">删除</el-button>
            <el-dropdown size="mini" type="text" icon="el-icon-edit">
              <span class="el-dropdown-link">
                <i class="el-icon-d-arrow-right">更多</i>
              </span>
              <el-dropdown-menu slot="dropdown">
                <el-dropdown-item icon="el-icon-circle-check">数据权限</el-dropdown-item>
                <el-dropdown-item icon="el-icon-user">分配用户</el-dropdown-item>
              </el-dropdown-menu>
            </el-dropdown>
          </template>
        </el-table-column>
      </el-table>
      <!-- 表格区域结束 -->

      <!-- 分页组件 -->
      <pagination
        :total="total"
        :background="false"
        :page-sizes="[1, 2]"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      ></pagination>
    </el-form>

      <el-dialog :title="title" :visible.sync="open" width="600px">
        <el-form label-width="100px" :model="form" ref="form" :rules="rules">
          <el-form-item label="角色名称" prop="roleName">
            <el-input placeholder="请输入角色名称" v-model="form.roleName"/>
          </el-form-item>

          <el-form-item prop="roleKey">
            <span slot="label">
              <el-tooltip content="控制器中定义的权限字符，如：@PreAuthorize(`@ss.hasRole('admin')`)">
                <i class="el-icon-question"></i>
              </el-tooltip>
              权限字符
            </span>
            <el-input placeholder="请输入权限字符" v-model="form.roleKey"/>
          </el-form-item>

          <el-form-item label="角色顺序" prop="roleSort">
            <el-input-number placeholder="请输入角色顺序" controls-position="right" :min="0" v-model="form.roleSort"/>
          </el-form-item>

          <el-form-item label="状态" prop="roleSort">
            <el-radio-group v-model="form.status">
              <el-radio label="0">正常</el-radio>
              <el-radio label="1">停用</el-radio>
            </el-radio-group>
          </el-form-item>

          <el-form-item label="菜单权限" label-width="100px">
            <el-checkbox v-model="menuExpand" @change="handleCheckedTreeExpand($event, 'menu')">展开/折叠</el-checkbox>
            <el-checkbox v-model="menuNodeAll" @change="handleCheckedTreeNodeAll($event, 'menu')">全选/全不选</el-checkbox>
            <el-checkbox v-model="menuCheckStrictly" @change="handleCheckedTreeConnect($event, 'menu')">父子联动</el-checkbox>
            <el-tree
              class="tree-border"
              empty-text="加载中，请稍后..."
              :data="menuOptions"
              show-checkbox
              :props="defaultProps"
              ref="menu"
              node-key="id"
              :check-strictly="!menuCheckStrictly"
            ></el-tree>
          </el-form-item>

          <el-form-item label="备注" prop="remark">
            <el-input type="textarea" placeholder="请输入备注" v-model="form.remark"/>
          </el-form-item>
        </el-form>
        <div slot="footer">
          <el-button type="primary" @click="submitForm">确定</el-button>
          <el-button @click="cancel">取消</el-button>
        </div>
      </el-dialog>

  </div>
</template>

<script>
import { listRole } from '@/api/system/role'
import { treeSelect as menuTreeSelect } from '@/api/system/menu'
export default {
  name: 'role',
  data () {
    return {
      queryParams: {
        roleName: undefined,
        roleKey: undefined,
        status: undefined,
        pageNum: 1,
        pageSize: 1
      },
      dateRange: [],
      roleList: [],
      total: 0,
      title: '',
      open: false,
      form: {},
      rules: {
        roleName: [
          { required: true, message: '角色名称不能为空', trigger: 'blur' }
        ],
        roleKey: [
          { required: true, message: '权限字符不能为空', trigger: 'blur' }
        ],
        roleSort: [
          { required: true, message: '角色排序不能为空', trigger: 'blur' }
        ]
      },
      menuOptions: [],
      defaultProps: {
        children: 'children',
        label: 'label'
      },
      menuExpand: false,
      menuNodeAll: false,
      menuCheckStrictly: false
    }
  },
  created () {
    this.getList()
  },
  methods: {
    handleUpdate (row) {
      console.log(row)
    },
    getList () {
      console.log('dateRange', this.dateRange)
      listRole(this.addDateRange(this.queryParams, this.dateRange)).then(res => {
        console.log(res)
        this.roleList = res.rows
        this.total = res.total
      })
    },
    handleQuery () {
      /**
       * 点搜索时重置页码
       */
      this.queryParams.pageNum = 1
      this.getList()
    },
    resetQuery () {
      this.dateRange = []
      this.resetForm('queryForm')
      this.handleQuery()
    },
    handleAdd () {
      this.reset()
      this.getMenuTreeSelect()
      console.log('handleAdd')
      this.open = true
      this.title = '添加角色'
    },
    cancel () {
      this.reset()
      this.open = false
    },
    submitForm () {
      this.$refs.form.validate(valid => {
        if (valid) {
          console.log('校验通过')
        }
      })
      console.log('form', this.form)
    },
    reset () {
      this.resetForm('form')
    },
    getMenuTreeSelect () {
      menuTreeSelect().then(res => {
        console.log('treeSelect', res)
        this.menuOptions = res.data
      })
    },
    handleCheckedTreeExpand (val, type) {
      if (type === 'menu') {
        const treeList = this.menuOptions
        for (let i = 0; i < treeList.length; i++) {
          this.$refs.menu.store.nodesMap[treeList[i].id].expanded = val
        }
        console.log(this.$refs.menu)
      }
    },
    handleCheckedTreeNodeAll (val, type) {
      if (type === 'menu') {
        this.$refs.menu.setCheckedNodes(val ? this.menuOptions : [])
      }
    },
    handleCheckedTreeConnect (val, type) {
      if (type === 'menu') {
        this.menuCheckStrictly = val
      }
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
